<?php
/**
 * GoodsSku
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/1/21
 * Time: 17:14
 */
namespace app\common\business;
use app\common\model\mysql\GoodsSku as GoodsSkuModel;

class GoodsSku extends BusBase
{
    public $model = null;
    public function __construct()
    {
        $this->model = new GoodsSkuModel();
    }

    /**
     * 批量新增逻辑
     */
    public function saveAll($data){
        if(!$data['skus']){
            return false;
        }
        foreach ($data['skus'] as $value){
            $insertData[] = [
                'goods_id'          => $data['goods_id'],
                'specs_value_ids'   => $value['propvalnames']['propvalids'],
                'price'             => $value['propvalnames']['skuSellPrice'],
                'cost_price'        => $value['propvalnames']['skuMarketPrice'],
                'stock'             => $value['propvalnames']['skuStock'],
            ];
        }
        try {
            $result = $this->model->saveAll($insertData);
            return $result->toArray();
        }catch (\Exception $e){
            return $e->getMessage();
            return false;
        }
    }

    /**
     * 根据skuId 关联获取sku和商品数据
     * @param $skuId
     * @return array|\think\Model|null
     */
    public function getNormalSkuAndGoods($skuId){
        try {
            $result = $this->model->with('goods')->find($skuId);
        }catch (\Exception $e){
            return [];
        }

        //注意：关联查询找不到返回 null
        if(!$result){
            return [];
        }
        $result = $result->toArray();
        if(!$result['goods']){
            return [];
        }
        if($result['goods']['status'] != config('status.mysql.table_normal')){
            return [];
        }
        return $result;
    }

    /**
     * 根据 goods_id获取sku信息
     * @param int $goodId
     * @return array
     */
    public function getSkusByGoodId($goodId = 0){
        if(!$goodId){
            return [];
        }
        try {
            $skus = $this->model->getSkusByGoodId($goodId);
        }catch (\Exception $e){
            return [];
        }
        return $skus->toArray();
    }
    public function getNormalInIds($ids){
        try {
            $result = $this->model->getNormalInIds($ids);
        }catch (\Exception $e){
            return [];
        }
        return $result ->toArray();
    }

    /**
     * 更新sku库存
     * @param $data
     * @return array|bool
     */
    public function updateStock($data){
        //使用 foreach 有性能瓶颈
//        foreach ($data as $value){
//            $this->model->decStrock($value['id'], $value['num']);
//        }
        //批量更新
        //获取skuID
        $updateList = [];
        foreach ($data as $sku){
            $updateList[] = [
                'id'    => $sku['id'],
                'stock' => $sku['stock'] - $sku['num']
            ];
        }
        try {
            $res = $this->model->saveAll($updateList);
        }catch (\Exception $e){
            return false;
        }

        return $res->toArray();
    }
}
